import { onMounted, reactive, toRefs } from 'vue'
import { useStore } from '@/store'
import Storage from '@/utils/storage'
import { ACCESS_ACTIVITYID } from '@/store/mutation-types'

export const useActivityId = () => {

  const store = useStore()


  const state = reactive({

  })
  // 获取活动id
  const getActivityId = () => {
    let res
    if (location.search) {
      const tempArr = location.search.split('?activityId=')
      res = tempArr[1].slice(0, tempArr[1].indexOf('/') > -1 ? tempArr[1].length - 1 : tempArr[1].length)
      store.commit('user/setActivityId', res)
      Storage.set(ACCESS_ACTIVITYID, res)
    }
    return res
  }
  // 截取地址栏数据
  //   function GetQueryString(name) {
  //     var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  //     var r = decodeURI(window.location.search.substr(1)).match(reg);
  //     if (r != null)return unescape(r[2]);
  //     return null;
  // }

  // var sname = GetQueryString("name");
  // if(sname!=null){
  //     var sname_ = decodeURIComponent(sname);
  //     alert(sname_);
  // }

  onMounted(async () => {

  })

  return {
    ...toRefs(state),
    getActivityId
  }
}
